import axios from 'axios'
import { getToken } from '@/utils/cookies'

const service = axios.create({
  baseURL: 'https://api-hmzs.itheima.net/api',
  timeout: 5000
})

// Add a request interceptor
service.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    const token = getToken()
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

// Add a response interceptor
service.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response.data
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error)
  }
)
export default service
